package com.fish.admin.module.system.controller;

import com.fish.admin.module.system.domain.dto.SysRoleDTO;
import com.fish.admin.module.system.domain.dto.SysRolePageQueryDTO;
import com.fish.admin.module.system.domain.vo.SysRoleVO;
import com.fish.admin.module.system.enums.AuthorityTypeEnum;
import com.fish.admin.module.system.enums.SubjectTypeEnum;
import com.fish.admin.module.system.service.SysAuthorityService;
import com.fish.admin.module.system.service.SysRoleService;
import com.fish.common.core.domain.vo.ItemVO;
import com.fish.common.core.domain.vo.PageVO;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.core.response.RestResponse;
import com.fish.common.support.util.EnumUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/role")
@RestController
@RequiredArgsConstructor
@Slf4j
public class SysRoleController {


    private final SysRoleService sysRoleService;

    private final SysAuthorityService sysAuthorityService;
    /**
     * 分页获取角色
     * @param sysRolePageQueryDTO
     * @return
     */
    @PostMapping("/v1/page")
    public RestResponse<PageVO<SysRoleVO>> page(
            @RequestBody SysRolePageQueryDTO sysRolePageQueryDTO) {
        return RestResponse.ok(sysRoleService.page(sysRolePageQueryDTO));
    }

    @PostMapping("/v1/changeEnabled/{id}/{enabled}")
    public RestResponse changeEnabled(@PathVariable Long id, @PathVariable Integer enabled) {
        sysRoleService.changeEnabled(id, EnumUtils.getEnum(enabled, EnabledEnum.class));
        return RestResponse.ok();
    }

    /**
     * @Title: save @Description: 新增
     * @param sysRoleDTO
     * @return RestResponse
     */
    @PostMapping("/v1/save")
    public RestResponse save(@RequestBody SysRoleDTO sysRoleDTO) {
        sysRoleService.save(sysRoleDTO);
        return RestResponse.ok();
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @PostMapping("/v1/delete/{id}")
    public RestResponse deleteById(@PathVariable Long id) {
        sysRoleService.deleteById(id);
        return RestResponse.ok();
    }

    /**
     * 根据id更新角色
     * @param id
     * @param sysRoleDTO
     * @return
     */
    @PostMapping("/v1/update/{id}")
    public RestResponse updateById(@PathVariable Long id,
                                   @RequestBody SysRoleDTO sysRoleDTO) {
        sysRoleService.updateById(id, sysRoleDTO);
        return RestResponse.ok();
    }

    /**
     * 角色列表
     * @return
     */
    @GetMapping("/v1/list")
    public RestResponse<List<ItemVO>> list() {
        return RestResponse.ok(sysRoleService.list());
    }
    /**
     * 角色授权
     * @param id
     * @param menuIds
     * @return
     */
    @PostMapping("/v1/grant/{id}")
    public RestResponse grant(@PathVariable Long id,
                              @RequestBody List<Long> menuIds) {
        sysAuthorityService.grant(id, SubjectTypeEnum.ROLE, menuIds, AuthorityTypeEnum.MENU);
        return RestResponse.ok();
    }
}
